<!-- 编辑弹窗 -->
<template>
  <!-- 高级弹窗modal -->
  <ele-modal
    form
    :width="600"
    v-model="visible"
    :title="isInfo ? '详情' : isUpdate ? '修改' : '添加'"
    @open="handleOpen"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="80px"
      :disabled="isInfo"
      @submit.prevent=""
    >
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="算法名称" prop="algorithmName">
            <el-input
              clearable
              v-model="form.algorithmName"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="算法代码" prop="algorithmCode">
            <el-input
              clearable
              v-model="form.algorithmCode"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="值类型" prop="valueType">
            <el-input clearable v-model="form.valueType" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="值" prop="value">
            <el-input clearable v-model="form.value" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备ID" prop="equId">
            <el-input clearable v-model="form.equId" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调用结果" prop="result">
            <el-input clearable v-model="form.result" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              v-model="form.startTime"
              type="date"
              placeholder="开始时间"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
              v-model="form.endTime"
              type="date"
              placeholder="结束时间"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="封面图" prop="cover">
            <el-upload
              :disabled="isInfo"
              class="avatar-uploader"
              action=""
              :show-file-list="false"
            >
              <img v-if="form.cover" :src="form.cover" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="视频演示效果" prop="video" label-width="120">
            <el-upload
              :disabled="isInfo"
              class="avatar-uploader"
              action=""
              :show-file-list="false"
            >
              <img v-if="form.video" :src="form.cover" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, nextTick } from 'vue';
  import { EleMessage, emailReg, phoneReg } from 'ele-admin-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { addDept, updateDept } from '@/api/system/dept';
  import { Plus } from '@element-plus/icons-vue';
  const props = defineProps({
    /** 修改回显的数据 */
    data: Object,
    /** 上级台账id */
    parentId: Number,
    isInfo: Boolean
  });

  const emit = defineEmits(['done']);

  /** 弹窗是否打开 */
  const visible = defineModel({ type: Boolean });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({});

  /** 关闭弹窗 */
  const handleCancel = () => {
    visible.value = false;
  };

  /** 保存编辑 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      const saveOrUpdate = isUpdate.value ? updateDept : addDept;
      saveOrUpdate({ ...form, parentId: form.parentId || 0 })
        .then((msg) => {
          loading.value = false;
          EleMessage.success(msg);
          handleCancel();
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error(e.message);
        });
    });
  };

  /** 弹窗打开事件 */
  const handleOpen = () => {
    if (props.data) {
      assignFields({
        ...props.data,
        parentId: props.data.parentId || void 0
      });
      isUpdate.value = true;
    } else {
      resetFields();
      form.parentId = props.parentId;
      isUpdate.value = false;
    }
    nextTick(() => {
      nextTick(() => {
        formRef.value?.clearValidate?.();
      });
    });
  };
</script>
<style lang="scss" scoped>
  :deep(.el-dialog) {
    width: 600px;
  }
</style>
<style scoped>
  .avatar-uploader .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    text-align: center;
  }
</style>
